package com.boatAnchorAssistant.service.repository.impl;

import com.boatAnchorAssistant.pojo.entity.Tenant;
import com.boatAnchorAssistant.service.mapper.TenantMapper;
import com.boatAnchorAssistant.service.repository.in.TenantRepository;
import nirvana.core.exceptions.BizException;
import nirvana.core.repository.Repository;
import org.springframework.stereotype.Service;
import com.boatAnchorAssistant.pojo.dto.tenant.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 *
 */
@Service
public class TenantRepositoryImpl extends Repository<TenantMapper, Tenant> implements TenantRepository {

    private final TenantMapper mapper;

    public TenantRepositoryImpl(TenantMapper mapper) {
        this.mapper = mapper;
    }


    @Override
    public Tenant createToBefore(Tenant tenant) {

        String name = tenant.getName();

        Tenant po = null;
        Tenant byPo = null;

        /**
         * 校验租户名称
         */
        po = new Tenant();
        po.setName(name);
        byPo = getByEntity(po);

        // TODO: 2022-06-27 此处需要 抛出一个再枚举中定义好的异常
        if (byPo != null) {
            throw new BizException("公司名称不能重复");
        }



        return super.createToBefore(tenant);
    }


    @Override
    public Tenant editToBefore(Tenant tenant) {

        String name = tenant.getName();
        Long id = tenant.getId();

        Tenant po = null;
        Tenant byPo = null;

        /**
         * 校验租户名称
         */
        po = new Tenant();
        po.setName(name);
        byPo = getByEntity(po);

        // TODO: 2022-06-27 此处需要 抛出一个再枚举中定义好的异常
        if (byPo != null && !byPo.getId().equals(id)) {
            throw new BizException("公司名称不能重复");
        }


        return super.editToBefore(tenant);
    }

    /**
     * 搜索企业
     * @param dto
     * @return
     */
    @Override
    public Page<Tenant> selectTenant(SelectTenantDto dto) {

        return mapper.selectTenant(dto);
    }

}